package com.speaktoitcookbook.dao.proxy;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.speaktoitcookbook.bean.User;
import com.speaktoitcookbook.bean.UserInfo;
import com.speaktoitcookbook.dao.IUserDao;
import com.speaktoitcookbook.dao.impl.UserDaoImpl;
import com.speaktoitcookbook.db.Database;

/**
 * 用户帐号操作代理类
 * 
 * @author 杨强
 * 
 */
public class UserDaoProxy implements IUserDao {
    private Connection conn;
    private IUserDao dao;

    public UserDaoProxy() {
        this.conn = Database.getConnection();
        this.dao = new UserDaoImpl(conn);
    }

    @Override
    public boolean doCreate(User user, UserInfo userInfo) throws SQLException {
        boolean result = false;
        if (null != user) {
            if (null != userInfo) {
                if (user.getID().equals(userInfo.getID())) {
                    try {
                        result = dao.doCreate(user, userInfo);
                    } catch (SQLException e) {
                        throw e;
                    } finally {
                        Database.close(conn);
                    }
                }
            } else {
                userInfo = new UserInfo();
                userInfo.setID(user.getID());
                try {
                    result = dao.doCreate(user, userInfo);
                } catch (SQLException e) {
                    throw e;
                } finally {
                    Database.close(conn);
                }
            }
        }
        return result;
    }

    @Override
    public boolean doUpdate(User user) throws SQLException {
        boolean result = false;
        if (null != user) {
            try {
                result = dao.doUpdate(user);
            } catch (SQLException e) {
                throw e;
            } finally {
                Database.close(conn);
            }
        }
        return result;
    }

    @Override
    public boolean doDelete(User user) throws SQLException {
        boolean result = false;
        if (null != user) {
            try {
                result = dao.doDelete(user);
            } catch (SQLException e) {
                throw e;
            } finally {
                Database.close(conn);
            }
        }
        return result;
    }

    @Override
    public boolean doDelete(String userID) throws SQLException {
        boolean result = false;
        if ((null != userID) && (!userID.isEmpty())) {
            try {
                result = dao.doDelete(userID);
            } catch (SQLException e) {
                throw e;
            } finally {
                Database.close(conn);
            }
        }
        return result;
    }

    @Override
    public User getUser(User user) throws SQLException {
        User result = null;
        if (null != user) {
            try {
                result = dao.getUser(user);
            } catch (SQLException e) {
                throw e;
            } finally {
                Database.close(conn);
            }
        }
        return result;
    }

    @Override
    public User getUser(String userID) throws SQLException {
        User result = null;
        if ((null != userID) && (!userID.isEmpty())) {
            try {
                result = dao.getUser(userID);
            } catch (SQLException e) {
                throw e;
            } finally {
                Database.close(conn);
            }
        }
        return result;
    }

    @Override
    public List<User> getUser(Object... params) throws SQLException {
        List<User> result = null;
        if (null != params) {
            try {
                result = dao.getUser(params);
            } catch (SQLException e) {
                throw e;
            } finally {
                Database.close(conn);
            }
        }
        return result;
    }
}
